<template>
  <div class="app-container">
    <el-card class="filter-container" shadow="never">
      <div>
        <i class="el-icon-search"></i>
        <span>筛选搜索</span>
        <el-button
          style="float: right"
          @click="searchMemberMealList()"
          type="primary"
          size="small"
          >查询结果</el-button
        >
      </div>
      <div style="margin-top: 15px">
        <el-form
          :inline="true"
          :model="listQuery"
          size="small"
          label-width="140px"
        >
          <el-form-item label="输入搜索：">
            <el-input
              style="width: 203px"
              v-model="listQuery.mealName"
              placeholder="套餐名称/关键字"
            ></el-input>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
    <el-card class="operate-container" shadow="never">
      <i class="el-icon-tickets"></i>
      <span>数据列表</span>
      <el-button class="btn-add" @click="addMemberMeal()" size="mini"
        >添加</el-button
      >
    </el-card>
    <div class="table-container">
      <el-table
        ref="memberMealTable"
        :data="list"
        style="width: 100%"
        @selection-change="handleSelectionChange"
        v-loading="listLoading"
        border
      >
        <el-table-column
          type="selection"
          width="60"
          align="center"
        ></el-table-column>
        <el-table-column label="编号" width="180" align="center">
          <template slot-scope="scope">{{ scope.row.id }}</template>
        </el-table-column>
        <el-table-column label="套餐名称" align="center">
          <template slot-scope="scope">{{ scope.row.mealName }}</template>
        </el-table-column>
        <el-table-column label="套餐原价" width="180" align="center">
          <template slot-scope="scope">{{ scope.row.sourcePrice }}</template>
        </el-table-column>
        <el-table-column label="套餐价格" width="180" align="center">
          <template slot-scope="scope">{{ scope.row.mealPrice }}</template>
        </el-table-column>
        <el-table-column label="一级佣金比例" width="180" align="center">
          <template slot-scope="scope">{{ scope.row.commissionRatio }}</template>
        </el-table-column>
        <el-table-column label="二级佣金比例" width="180" align="center">
          <template slot-scope="scope">{{ scope.row.twoCommissionRatio }}</template>
        </el-table-column>
        <el-table-column label="套餐图片" width="180" align="center">
          <template slot-scope="scope"
            ><img style="height: 80px" :src="scope.row.pic"
          /></template>
        </el-table-column>
        <el-table-column label="套餐时长" width="180" align="center">
          <template slot-scope="scope">{{ scope.row.mealExpiredate }}</template>
        </el-table-column>
        <el-table-column label="操作" width="200" align="center">
          <template slot-scope="scope">
            <el-button
              size="mini"
              @click="handleUpdate(scope.$index, scope.row)"
              >编辑
            </el-button>
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="batch-operate-container"></div>
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes,prev, pager, next,jumper"
        :page-size="listQuery.pageSize"
        :page-sizes="[5, 10, 15]"
        :current-page.sync="listQuery.pageNum"
        :total="total"
      ></el-pagination>
    </div>
  </div>
</template>
<script>
import { formatDate } from "@/utils/date";
import { fetchList, deleteMemberMeal } from "@/api/ums/membermeal";

export default {
  name: "memberMealList",
  data() {
    return {
      operates: [
        {
          label: "显示会员套餐",
          value: "showMemberMeal",
        },
        {
          label: "隐藏会员套餐",
          value: "hideMemberMeal",
        },
      ],
      operateType: null,
      listQuery: {
        mealName: null,
        pageNum: 1,
        pageSize: 10,
      },
      list: null,
      total: null,
      listLoading: true,
      multipleSelection: [],
    };
  },
  created() {
    this.getList();
  },
  filters: {
    formatTime(time) {
      if (time == null || time === "") {
        return "N/A";
      }
      let date = new Date(time);
      return formatDate(date, "yyyy-MM-dd hh:mm:ss");
    },
  },
  methods: {
    getList() {
      this.listLoading = true;
      fetchList(this.listQuery).then((response) => {
        this.listLoading = false;
        this.list = response.data.records;
        this.total = response.data.total;
        this.totalPage = response.data.pages;
        this.pageSize = response.data.size;
      });
    },
    addMemberMeal() {
      this.$router.push({ path: "/ums/addMemberMeal" });
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    handleSizeChange(val) {
      this.listQuery.pageNum = 1;
      this.listQuery.pageSize = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.listQuery.pageNum = val;
      this.getList();
    },
    searchMemberMealList() {
      this.listQuery.pageNum = 1;
      this.getList();
    },
    handleUpdate(index, row) {
      this.$router.push({
        path: "/ums/updateMemberMeal",
        query: { id: row.id },
      });
    },
    handleDelete(index, row) {
      this.$confirm("是否要删除该会员套餐", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        deleteMemberMeal(row.id).then((response) => {
          this.$message({
            message: "删除成功",
            type: "success",
            duration: 1000,
          });
          this.getList();
        });
      });
    },
  },
};
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
</style>


